package com.publish.file.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.publish.file.filter.JwtAuthFilter;
import com.publish.file.service.auth.AuthAccessDeniedHandler;
import com.publish.file.service.auth.UserRoleManagerService;
import com.publish.file.service.auth.UserRoleSecurityProvider;
import com.publish.file.service.jwt.JwtService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class IgnoreSecurityConfiguration extends WebSecurityConfigurerAdapter {
	@Autowired
	UserRoleManagerService userRoleManagerService;
	@Autowired
	JwtService jwtServiceImpl;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().cors().disable()
				.addFilterBefore(new JwtAuthFilter(super.authenticationManager(), jwtServiceImpl),
						UsernamePasswordAuthenticationFilter.class)
				.exceptionHandling().accessDeniedHandler(new AuthAccessDeniedHandler()).and().authorizeRequests()
				.antMatchers("/auth/*").permitAll().anyRequest().permitAll();
		http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userRoleManagerService);
		auth.authenticationProvider(new UserRoleSecurityProvider());
	}
}
